package com.mairuide._frame.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-01-25 10:32
 * 判断网络是否正常
 */
public class NetworkConnect {
    public static boolean checkNetwork(){
        Runtime runtime = Runtime.getRuntime();
        try {
            Process process = runtime.exec("ping " + "www.baidu.com");
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            is.close();
            isr.close();
            br.close();

            if (null != sb && !sb.toString().equals("")) {
                if (sb.toString().indexOf("TTL") > 0) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    	/*try {  这个方法需要等十几秒  有时候还卡死！！！！！
    		 URL url = new URL("http://baicu.com/");
	         InputStream in = url.openStream();//打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream
	         in.close();//关闭此输入流并释放与该流关联的所有系统资源。
	         return true;
	    } catch (IOException e) {
	    }*/
        return false;
    }
}
